package com.myexam.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.myexam.dao.SubjectDao;
import com.myexam.model.Subject;
import com.myexam.service.SubjectService;
import com.myexam.util.Page;
import com.myexam.util.PageResult;
import com.myexam.util.PageUtil;
@Service("subjectService")
public class SubjectServiceimpl implements SubjectService {
	@Autowired
	private SubjectDao subjectDao;

	@Override
	public boolean saveSubjcet(Subject subject) {
		// TODO Auto-generated method stub
		String subjectTitle = subject.getSubjectTitle();
		if(subjectDao.findSubjectByTitle(subjectTitle).size() == 0){
			subjectDao.addSubject(subject);
			return true;
		}
		return false;
	}
	


	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public PageResult querySubjectByPage(Page page) {
		// TODO Auto-generated method stub
		page = PageUtil.createPage(page.getEveryPage(), subjectDao.findSubjectCount(), page.getCurrentPage());
		List<Subject> list = subjectDao.findSubjectByPage(page);
		PageResult pageResult = new PageResult(page, list);
		return pageResult;
	}
	/*@SuppressWarnings("rawtypes")
	@Override
	public PageResult likeQueryBySubjectTitle( Page page) {
		// TODO Auto-generated method stub
		page = PageUtil.createPage(page.getEveryPage(), subjectDao.findSubjectCount(), page.getCurrentPage());
		List<Subject> list = subjectDao.likeQueryByTitle(page);
		PageResult<Subject> pageResult = new PageResult<>(page,list);
		return pageResult;
	}*/

	@Override
	public Subject showSubjectParricular(int subjectID) {
		// TODO Auto-generated method stub
		return subjectDao.findSubjectByID(subjectID);
	}

	@Override
	public void updateSubject(Subject subject) {
		// TODO Auto-generated method stub
		subjectDao.updateSubject(subject);
	}

	@Override
	public void deleteSubject(int subjectID) {
		// TODO Auto-generated method stub
		subjectDao.deleteSubject(subjectID);
	}

	

	@Override
	public List<Subject> randomFindSubject(int number) {
		// TODO Auto-generated method stub
		return subjectDao.randomFindSubject(number);
	}

	@Override
	public int accountResult(List<Integer> subjectIDs,
			List<String> studentAnsers) {
		// TODO Auto-generated method stub
		int generalPoint = 0;
		for(int i = 0; i < subjectIDs.size(); i ++){
			String rightAnswerString = subjectDao.findSubjectByID(subjectIDs.get(i)).getSubjectAnswer();
			if(rightAnswerString.equals(studentAnsers.get(i))){
				generalPoint +=5;
			}
		}
		return generalPoint;
	}

}
